﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoGame;
using SoGame.CommandManager;
using System.Threading;
using EventAggregator;
using SoGame.WebServices.Host.Messages;

namespace SoGame.WebServices.Host
{
    public partial class ServiceManager : Form, IHandle<ChangeStateMessage>
    {
        private CommandManager.CommandManager commandManager = new CommandManager.CommandManager();
        private EventAggregator.EventAggregator eventAggregator = new EventAggregator.EventAggregator();
        private ServiceStatusModel model;

        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceManager"/> class.
        /// </summary>
        public ServiceManager()
        {
            InitializeComponent();

            model = new ServiceStatusModel(eventAggregator);

            eventAggregator.Subscribe(this);

            this.serviceNameLabel.DataBindings.Add("Text", model, "Name");
            this.baseAddressesListBox.DataSource = model.BaseAddresses;

            this.commandManager.Bind(model.StartServiceCommand, this.startButton);
            this.commandManager.Bind(model.StopServiceCommand, this.stopButton);

            this.notifyIcon1.Icon = this.Icon;
        }

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Form.Closing"/> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs"/> that contains the event data.</param>
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            this.notifyIcon1.Visible = false;
            eventAggregator.Unsubscribe(this);
        }

        /// <summary>
        /// Handles the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public new void Handle(ChangeStateMessage message)
        {
            var msg = string.Empty;

            switch (message.State)
            {
                case System.ServiceModel.CommunicationState.Closed:
                    msg = "{0} service was closed.";
                    break;
                case System.ServiceModel.CommunicationState.Closing:
                    msg = "{0} service is closing.";
                    break;
                case System.ServiceModel.CommunicationState.Created:
                    msg = "{0} service was created.";
                    break;
                case System.ServiceModel.CommunicationState.Faulted:
                    msg = "{0} service was faulted.";
                    break;
                case System.ServiceModel.CommunicationState.Opened:
                    msg = "{0} service was opened.";
                    break;
                case System.ServiceModel.CommunicationState.Opening:
                    msg = "{0} service is opening.";
                    break;
                default:
                    break;
            }

            this.notifyIcon1.BalloonTipText = msg.Format(new object[] {model.Name});
            this.notifyIcon1.ShowBalloonTip(3000);
        }
    }
}
